package handler

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	"video-software/api/request"
	"video-software/global"
	"video-software/internal/dao"
	"video-software/internal/model"
)

func AddBarrage(c *gin.Context) {
	var req request.AddBarrageForm
	var barrage model.Barrage

	if err := c.ShouldBind(&req); err != nil {
		return
	}

	if req.UserId == 0 || req.VideoId == 0 || req.Info == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	token := c.GetHeader("x-token")

	get := global.RDB.Get(context.Background(), fmt.Sprintf("token:%d", token)).Val()

	if get != strconv.Itoa(int(req.UserId)) {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "发送失败:未登录",
			"data": nil,
		})
		return
	}

	barrage = model.Barrage{
		UserId:  req.UserId,
		VideoId: req.VideoId,
		Info:    req.Info,
	}

	err := dao.AddFile(&barrage)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "发送失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "发送成功",
		"data": barrage.Info,
	})

	global.RDB.Set(context.Background(), "barrage", barrage.Info, time.Hour*24)

}

func ListBarrage(c *gin.Context) {

}

func FinListBarrage(c *gin.Context) {
	
}

func DeleteBarrage(c *gin.Context) {
	
}